var uid = 1;
var countdown = 60; //计时60s
var countTimer;
layui.use(['table', 'form', 'laydate'], function() {
	var table = layui.table,
		$ = layui.jquery,
		layer = layui.layer,
		form = layui.form,
		laydate = layui.laydate;

	//复选框样式
	ckBoxColor();
	//初始化查询参数
	var options = {
		offset: 0,
		limit: 10,
		pageIndex: 1,
	};
	var tempOptions = options;
	loadData(options);
	//点击禁用
	$(".searchTools .del-btn").on("click", function() {
		var ck = $(".tableList .ckBox.on");
		if(ck.length <= 0) {
			layer.msg("请选择一条数据！");
			return;
		}
		if(ck.length > 1) {
			layer.msg("只能选择一条数据！");
			return;
		}
		var id = ck.attr("data-id");
		//询问框
		layer.confirm('确定要禁用吗？', {
			icon: 3,
			btn: ['确定', '取消'] //按钮
		}, function() {
			var loading = layer.load(1);
			$.ajax({
				type: "PUT",
				url: "/sys_btn/delete/" + id,
				async: true,
				dataType: 'json',
				timeout: 30000,
				data: {
					id: id
				},
				success: function(res) {
					layer.close(loading);
					// console.log(res);
					if(res.code == 0) {
						layer.msg(res.msg);
						loadData(tempOptions);
					} else {
						layer.msg(res.msg);
						return;
					}
				},
				error: function(res) {
					layer.msg("请求失败，请稍后再试！");
					layer.close(loading);
					return;
				},
				complete: function(XMLHttpRequest, status) {
					if(status == 'timeout') {
						layer.msg("请求超时，请稍后再试！");
						layer.close(loading);
						return;
					}

				}
			});
		}, function() {
			return;
		});
	});

	//点击启用
	$(".searchTools .on-btn").on("click", function() {
		var ck = $(".tableList .ckBox.on");
		if(ck.length <= 0) {
			layer.msg("请选择一条数据！");
			return;
		}
		if(ck.length > 1) {
			layer.msg("只能选择一条数据！");
			return;
		}
		var id = ck.attr("data-id");
		//询问框
		layer.confirm('确定要启用吗？', {
			icon: 3,
			btn: ['确定', '取消'] //按钮
		}, function() {
			var loading = layer.load(1);
			$.ajax({
				type: "PUT",
				url: "/sys_btn/enabled/" + id,
				async: true,
				dataType: 'json',
				timeout: 5000,
				data: {
					id: id
				},
				success: function(res) {
					layer.close(loading);
					// console.log(res);
					if(res.code == 0) {
						layer.msg(res.msg);
						loadData(tempOptions);
					} else {
						layer.msg(res.msg);
						return;
					}
				},
				error: function(res) {
					layer.msg("请求失败，请稍后再试！");
					layer.close(loading);
					return;
				},
				complete: function(XMLHttpRequest, status) {
					if(status == 'timeout') {
						layer.msg("请求超时，请稍后再试！");
						layer.close(loading);
						return;
					}

				}
			});
		}, function() {
			return;
		});
	})

	//点击添加
	$(".searchTools .add-btn").on("click", function() {
		$(".addModal").show();
		//		initPID(); //初始化父级菜单
	})
	$(".addModal .close").on("click", function() {
		$(".addModal").hide();
		$(".addModal input").val("");
		$(".addModal select").val("");
		$(".addModal textarea").val("");
		form.render();
	})
	//点击添加提交
	form.on('submit(addBtns)', function() {
		var name = $.trim($(".addModal .name").val());
		var status = $.trim($(".addModal .status").val());
		var btnType = $.trim($(".addModal .btnType").val());
		var status = $.trim($(".addModal .status").val());
		var sortby = $.trim($(".addModal .sortby").val());
		var remark = $.trim($(".addModal .remark").val());

		if(name == '' || name == null || name == undefined) {
			layer.msg("请输入按钮名称！");
			return;
		}
		var options = {
			name: name,
			status: status,
			btnType: btnType
		};
		if(remark != null && remark != undefined && remark != "") {
			options['remark'] = remark;
		}
		//		console.log(options);
		var loading = layer.load(1);
		$.ajax({
			type: "put",
			url: "/sys_btn/save",
			async: true,
			dataType: 'json',
			timeout: 5000,
			data: options,
			success: function(res) {
				layer.close(loading);
				// console.log(res);
				if(res.code == 0) {
					layer.msg(res.msg);
					$(".addModal").hide();
					$(".addModal input").val("");
					$(".addModal select").val("");
					$(".addModal textarea").val("");
					form.render();
					loadData(tempOptions);
				} else {
					layer.msg(res.msg);
					return;
				}
			},
			error: function(res) {
				layer.msg("请求失败，请稍后再试！");
				layer.close(loading);
				return;
			},
			complete: function(XMLHttpRequest, status) {
				if(status == 'timeout') {
					layer.msg("请求超时，请稍后再试！");
					layer.close(loading);
					return;
				}

			}
		});
	})
	//修改按钮
	$(".searchTools .set-btn").on("click", function() {
		var ck = $(".tableList .ckBox.on");
		if(ck.length > 1) {
			layer.msg("只能选取一条数据！");
			return;
		}
		if(ck.length <= 0) {
			layer.msg("请选取一条数据！");
			return;
		}
		$(".updateModal").show();
		var info = ck.parent().parent();
		$(".updateModal .name").val($(info).children().eq(2).text());
		$(".updateModal .btnType").val($(info).children().eq(3).attr("data-btnType"));
		$(".updateModal .remark").val($(info).children().eq(5).text());
		$(".updateModal .status").val($(info).children().eq(4).attr("data-status"));
		form.render();
	})
	$(".updateModal .close").on("click", function() {
		$(".updateModal").hide();
		$(".updateModal input").val("");
		$(".updateModal select").val("");
		$(".updateModal textarea").val("");
		form.render();
		$("body .ckBox").removeClass("on");
	})
	//按钮修改
	form.on('submit(updateBtns)', function() {
		var ck = $(".tableList .ckBox.on");
		var id = ck.attr("data-id");
		var name = $.trim($(".updateModal .name").val());
		var status = $.trim($(".updateModal .status").val());
		var btnType = $.trim($(".updateModal .btnType").val());
		var remark = $.trim($(".updateModal .remark").val());
		if(name == '' || name == null || name == undefined) {
			layer.msg("请输入菜单名称！");
			return;
		}

		var options = {
			id: parseInt(id),
			name: name,
			btnType: btnType,
			status: status,
		};
		if(remark != null && remark != undefined && remark != "")
			options['remark'] = remark;


		var loading = layer.load(1);
		$.ajax({
			type: "put",
			url: "/sys_btn/update",
			async: true,
			dataType: 'json',
			timeout: 5000,
			data: options,
			success: function(res) {
				layer.close(loading);
				// console.log(res);
				if(res.code == 0) {
					layer.msg(res.msg);
					$(".updateModal").hide();
					$(".updateModal input").val("");
					$(".updateModal select").val("");
					$(".updateModal textarea").val("");
					form.render();
					loadData(tempOptions);
				} else {
					layer.msg(res.msg);
					return;
				}
			},
			error: function(res) {
				layer.msg("请求失败，请稍后再试！");
				layer.close(loading);
				return;
			},
			complete: function(XMLHttpRequest, status) {
				if(status == 'timeout') {
					layer.msg("请求超时，请稍后再试！");
					layer.close(loading);
					return;
				}

			}
		});
	})

	//获取数据列表
	function loadData(options) {
		var loading = layer.load(1);
		$.ajax({
			type: "POST",
			url: "/sys_btn/list",
			async: true,
			dataType: 'json',
			timeout: 5000,
			data: options,
			success: function(res) {
				layer.close(loading);
								// console.log(res);
				if(res.code == 0) {
					$(".menuListPage").page({
						total: res.data.total, //总条数
						pageIndex: options.pageIndex, //当前页
						pageSize: options.limit, //每页显示
						showInfo: true, //是否显示记录信息
						prevBtnText: '上一页',
						nextBtnText: "下一页",
						infoFormat: '总共{total}条记录,每页显示{size}条记录,共{count}页' //定义记录信息格式
					}).on("pageClicked", function(event, pageIndex) {
						//分页点击
						options.offset = options.offset - (options.pageIndex-pageIndex )* options.limit;
						options.pageIndex = pageIndex;
						loadData(options);
					});

					//具体数据列表
					var data = res.data.records;
					if(data == null || data.length <= 0) {
						$(".menuList .tableList").empty().append("<tr><td colspan='6'>暂无数据</td></tr>");
						return;
					}
					var html = '';
					$.each(data, function(index, val) {
						var statusStr = val.status == 0 ? '正常' : '禁用';
						var typeStr = val.btnType == 0 ? '查询按钮' : '增删改按钮';
						html += "<tr><td><span class='ckBox' data-id='" + val.id + "'></span></td><td>" + val.id + "</td><td>" + val.name + "</td>" +
							"<td data-type='" + val.btnType + "'>" + typeStr + "</td><td data-status='" + val.status + "'>" + statusStr + "</td><td>" + val.remark + "</td></tr>";
					});
					$(".menuList .tableList").empty().append(html);
					replaceNull($(".menuList .tableList"));
				} else {
					layer.msg(res.msg);
					return;
				}
			},
			error: function(res) {
				layer.msg("请求失败，请稍后再试！");
				layer.close(loading);
				return;
			},
			complete: function(XMLHttpRequest, status) {
				if(status == 'timeout') {
					layer.msg("请求超时，请稍后再试！");
					layer.close(loading);
					return;
				}

			}
		});

	}

	/**显示值转换**/
	function convertVal(val) {
		if (val == null || val == '' || val == undefined)
			return '';
		else
			return val;
	}
	
});